from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from info import db,create_app,models



# 创建 app，并传入配置模式：development / production
from info.models import User

app = create_app("development")

# 创建迁移的对象
manager = Manager(app)

Migrate(app,db)
manager.add_command("db",MigrateCommand)


# 添加管理员--使用终端命令创建
@manager.option("-n","-name",dest = "name")
@manager.option("-p","-password",dest = "password")
def createsuperuser(name,password):
    if not all ([name,password]):
        print("参数不足")
    # 初始化一个管理员对象
    user = User()
    user.nick_name = name
    user.mobile = name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        print(e)
        print("创建成功")



# @app.route("/")
# def index():
#     # session["name"] = "itheima"
#     # 测试打印log日志
#     logging.debug("This is a debug log.")
#     logging.info("This is a info log.")
#     logging.warning("This is a warning log.")
#     logging.error("This is a error log.")
#     logging.critical("This is a critical log.")
#
#     # 测试打印log日志
#     current_app.logger.error("测试error")
#     return "11"


if __name__ == "__main__":
    manager.run()